﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace AlarmManagePlatform.UC
{
    public partial class Map : UserControl
    {
        public Map()
        {
            InitializeComponent();
            webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "Resources\\Baidu.html"));
            webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;


            cmbType.DropDownStyle = ComboBoxStyle.DropDown;
            cmbType.DisplayMember = "Description";
            cmbType.ValueMember = "value";


            List<RelationsLMP.Model.ObjectInfo> _List = new List<RelationsLMP.Model.ObjectInfo>()
            {
                new RelationsLMP.Model.ObjectInfo()
                {
                    Description = "送检单位",
                    value = "SubmitUnit"
                },
                new RelationsLMP.Model.ObjectInfo()
                {
                    Description = "报警器",
                    value = "AlarmInstrument"
                },
                //new RelationsLMP.Model.ObjectInfo()
                //{
                //    Description = "采集终端",
                //    value = "CollectTerminal"
                //}
            };
            cmbType.DataSource = _List;

            if (cmbType.Items.Count > 0)
            {
                cmbType.SelectedValueChanged += CmbType_SelectedValueChanged;
                cmbType.SelectedIndex = 0;
            }
        }

        private void CmbType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbType.SelectedItem == null) return;

            UpdateMap(cmbType.SelectedValue.ToString());
        }

        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            btnOnload.PerformClick();
        }

        private void btnOnload_Click(object sender, EventArgs e)
        {
            webBrowser1.Document.InvokeScript("clearMarkerAll", new object[] { });

            List<Model.DB.submitUnitGPSInfo> submitUnitGPSInfos = new DataAccess.tb_SubmitUnit().getSubmitUnitGPSInfo();
            for (int i = 0; i < submitUnitGPSInfos.Count; i++)
            {
                webBrowser1.Document.InvokeScript("addMarker", new object[] { submitUnitGPSInfos[i].latitude, submitUnitGPSInfos[i].longitude,
                    "名称：" + submitUnitGPSInfos[i].name + "<br>" + "地址：" + submitUnitGPSInfos[i].GpsDesc });
            }
        }

        private void UpdateMap(string type)
        {

            webBrowser1.Document.InvokeScript("clearMarkerAll", new object[] { });

            List<Model.DB.GPSInfo> _List;
            _List = new List<Model.DB.GPSInfo>();
            switch (type)
            {
                case "SubmitUnit":

                    //送检单位

                    _List = new DataAccess.tb_SubmitUnit().GetGPSInfo();

                    break;

                case "AlarmInstrument":

                    //报警器

                    _List = new DataAccess.tb_AlarminstrumentInfo().GetGPSInfo();

                    break;

                case "CollectTerminal":

                    //采集终端

                    _List = new DataAccess.tb_DataCollectTerminalInfo().GetGPSInfo();

                    break;

                default:

                    _List.Clear();
                    break;
            }

            for (int i = 0; i < _List.Count; i++)
            {
                webBrowser1.Document.InvokeScript("addMarker", new object[] { _List[i].Latitude, _List[i].Longitude,
                    "名称：" + _List[i].Name + "<br>" + "地址：" + _List[i].GpsDesc });
            }
        }
    }
}